package main

import (
	"gitee.com/cjf8134/douyin/utils/constutil"
	"gitee.com/cjf8134/douyin/utils/functionutil"
	"gitee.com/cjf8134/douyin/utils/types"
	"log"
	"strconv"
	"strings"
	"time"
)

// main 入口
func main() {
	initDouYin()
}

// initDouYin 初始化
func initDouYin() {
	videoList := make([]types.VideoList, 0)
	startTime := constutil.StartTime
	currentTime := time.Now().UnixNano() / 1e6
	for startTime < currentTime {
		timeStr := strconv.FormatInt(startTime, 10)
		url := strings.Replace(strings.Replace(strings.Replace(constutil.RequestUrl, "{$max_cursor}", timeStr, 1), "{$sec_uid}", constutil.SecUid, 1), "{$_signature}", constutil.Signature, 1)
		log.Println("url === ", url)
		resp, err := functionutil.RequestClient(url)
		if err != nil {
			return
		}
		if len(resp.AwemeList) == 0 {
			return
		}
		for _, v := range resp.AwemeList {
			lastUrl := v.Video.PlayAddr.UrlList[len(v.Video.PlayAddr.UrlList)-1]
			exist := false
			for _, vv := range videoList {
				if vv.Url == lastUrl {
					exist = true
				}
			}
			if !exist {
				var video types.VideoList
				video.AwemeId = v.AwemeId
				video.Url = v.Video.PlayAddr.UrlList[len(v.Video.PlayAddr.UrlList)-1]
				videoList = append(videoList, video)
			}
		}
		log.Printf("时间：%d，视频数：%d \n", startTime, len(videoList))
		startTime = startTime + 24*60*60*1000
	}
	for _, v := range videoList {
		_ = functionutil.VideoDownload(v)
	}
	return
}
